package deployr.maven.plugin;

import static deployr.core.Files.*;
import static java.text.MessageFormat.*;

import org.apache.maven.plugin.*;
import org.apache.maven.project.*;

import deployr.client.api.*;
import deployr.client.http.*;
import deployr.core.*;

/**
 * @goal update
 */
public class UpdateMojo extends AbstractMojo {
	/**
	 * @parameter appDir
	 * @required
	 */
	private String appDir;

	/**
	 * @parameter deployrHost
	 */
	private String host = "localhost";

	/**
	 * @parameter deployrPort
	 */
	private String port = "8089";

	/**
	 * @parameter expression="${project}"
	 * @required
	 */
	private MavenProject project;

	public UpdateMojo() {}
	

	public UpdateProcess createUpdate(String host, String port, String appId) {
		return new UpdateProcess(new Deployr(format("http://{0}:{1}", host, port)), appId, "");
	}

	public void execute() throws MojoExecutionException, MojoFailureException {
		Path explodedPath = new Path(userDir(""), appDir);
		String appId = explodedPath.getName();
		UpdateProcess updateProcess = createUpdate(host, port, appId);
		updateProcess.update(explodedPath);

	}

	public void setProject(MavenProject project) {
		this.project = project;
	}

	public void setAppDir(String appDir) {
		this.appDir = appDir;
	}

	public void setHost(String host) {
		this.host = host;
	}
	
	public void setPort(String port) {
		this.port = port;
	}
}